<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/8/27
 * Time: 18:20
 */

namespace App\Services;


class Behavior
{
    /**
     * @param int $uid  用户id
     * @param int $media_id  视频id
     * @param int $action  动作 （0：点赞  1：分享  2：浏览时长 99:无动作）
     * @param int $duration  浏览时长 （仅当action为浏览时长时传该值）
     * @param int $mobileMarkId  设备号表id
     * @param string $province  省
     * @param string $city  市
     * @return void
     */
    public static function write($uid = 0, $media_id = 0, $action = 0, $duration = 0, $mobileMarkId=0, $province=0, $city=0)
    {
        $log_file = env('BEHAVIOR_LOG_PATH') . 'behaviors' . date('Ymd') . '.log';
        if (!is_dir(dirname($log_file))) {
            return;
        }

        $time = time();
        $content = "{$uid}\t{$media_id}\t{$action}\t{$duration}\t{$mobileMarkId}\t{$province}\t{$city}\t{$time}\n";
        file_put_contents($log_file, $content, FILE_APPEND);
    }

    /*
     * 过滤推荐
     * */
    public static function filtr_recommend_write($params, $file_name)
    {
        if(empty(trim($params['did'])) && (trim($params['did'])) != '0')
            return ['status'=>'error', 'message_code'=>10138, 'message'=>'无效数据'];
        if($params['uid'] == '0' && $params['did'] == '0')
            return ['status'=>'error', 'message_code'=>10138, 'message'=>'无效数据'];

        $path = env('BEHAVIOR_LOG_PATH').$file_name;
        $log_file = $path.'/'.date('Ymd').'.log';

        // 创建目录
        if (!is_dir($path)) {
            $res = mkdir(iconv("UTF-8", "GBK", $path), 0777, true);
            if (!$res)
                return ['status' => 'error', 'message_code'=>10139, 'message' => '目录不存在'];
        }

        $time = time();
        $content = "{$params['uid']}\t{$params['did']}\t{$params['media_ids']}\t{$time}\n";
        $put = file_put_contents($log_file, $content, FILE_APPEND);
        if($put)
            return true;
        return false;
    }

}